using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Mail;

namespace WebLog.Extends
{
    internal class EMailExtend : ExtendBase
    {
        const string def_uid = "";
        const string def_pwd = "";
        internal static object SendEmail(string subject, string body)
        {
            if (WebLogHelper.GetStatus() < WebLogStatus.Runed)
                return Comm.TypeFactory.CreateBaseBizResult("日志服务还未启动，可能原因：正在升级数据结构中！");
            var uid = repository.Get<string>("EmailUID", def_uid);
            var pwd = repository.Get<string>("EmailPWD", def_pwd);
            SendEmail(uid, pwd, uid, subject ?? "意见反馈", body);
            return null;
        }
        static void SendEmail(string fromAddr, string psw, string toAddr, string subject, string body)
        {
            MailMessage myMail = new MailMessage();
            myMail.From = new MailAddress(fromAddr);
            myMail.To.Add(new MailAddress(toAddr));

            myMail.Subject = subject;
            myMail.SubjectEncoding = Encoding.UTF8;

            myMail.BodyEncoding = Encoding.UTF8;
            myMail.IsBodyHtml = true;
            if (myMail.IsBodyHtml)
                body = body.Replace("\r\n", "<br/>");
            myMail.Body = body;

            SmtpClient smtp = new SmtpClient();
            smtp.Host = "smtp.qq.com";
            smtp.Credentials = new NetworkCredential(fromAddr, psw);
            Comm.TryRun(() => smtp.Send(myMail));
        }
        public class EMailInputDto
        {
            /// <summary>
            /// 标题
            /// </summary>
            public string Subject { get; set; }
            /// <summary>
            /// 正文
            /// </summary>
            public string Body { get; set; }
            /// <summary>
            /// 秘钥
            /// </summary>
            public string PWD { get; set; }
        }
    }
}
